
	foreach framename in grades_45 grades_45_no_exp0 grades_456 grades_456_no_exp0 grade_3_no_exp0 {

		
		frame copy original_data `framename'
		frame `framename' {
			foreach var in pr6 pr6_p pr6_m pr6_1p pr6_2p pr6_3p pr6_1m pr6_2m pr6_1p_std pr6_2p_std pr6_3p_std pr6_1m_std pr6_2m_std {
				sum `var', meanonly
				local mymean = `r(mean)'
				gen _miss_`var' = mi(`var')
				replace `var' = `mymean' if _miss_`var'
				}
				
			* create subsample:
			if "`framename'" == "grades_456" keep if inlist(grade, "4", "5", "6") & (ivamath != . | ivaread != .)
			if "`framename'" == "grades_456_no_exp0" keep if inlist(grade, "4", "5", "6") & !exp0 & (ivamath != . | ivaread != .)
			if "`framename'" == "grades_45" keep if inlist(grade, "4", "5") & (ivamath != . | ivaread != .)
			if "`framename'" == "grades_45_no_exp0" keep if inlist(grade, "4", "5") & !exp0 & (ivamath != . | ivaread != .)
			if "`framename'" == "grade_3_no_exp0" {
			    keep if grade == "3" & !exp0 & year == 2012
				global prevvars
			}
			
			keep if year_dose > $dosemin
			
			egen _id_year = concat(id year)
			
			*egen year_dose_cat = cut(year_dose), at(0(0.1)1.1) icodes
			egen year_dose_cat = cut(year_dose), at(0, 0.25, 0.5, 0.75, 1, 1.1) icodes
			
			egen ntc1_cat = cut(nothreat_cycle1_true), at(0,1,5,10,15,20,25,30,35,40,45,50)
			egen ntc2_cat = cut(nothreat_cycle2), at(0,1,5,10,15,20,25,30,35,40)
			egen ntc3_cat = cut(nothreat_cycle3), at(0,1,5,10,15,20,25,30)
			
			
			
			gen zero_ntc4 = nothreat_cycle4 == 0
			gen m2_before_test = nothreat_m2 > 0
			gen p2_before_test = nothreat_p2 > 0
				
			gen p1_order = .
			gen p2_order = .
			gen p3_order = .
			gen m1_order = .
			gen m2_order = .

			replace p1_order = 1 if obsdate_1p < obsdate_1m
			replace p1_order = 2 if obsdate_1p > obsdate_1m

			replace m1_order = 1 if obsdate_1p > obsdate_1m
			replace m1_order = 2 if obsdate_1p < obsdate_1m & obsdate_1m < obsdate_2p
			replace m1_order = 3 if obsdate_1m > obsdate_2p

			replace p2_order = 2 if obsdate_2p < obsdate_1m
			replace p2_order = 3 if obsdate_1m < obsdate_2p & obsdate_2p < obsdate_2m
			replace p2_order = 4 if obsdate_2p > obsdate_2m

			replace m2_order = 3 if obsdate_2m < obsdate_2p
			replace m2_order = 4 if obsdate_2p < obsdate_2m & obsdate_2m < obsdate_3p
			replace m2_order = 5 if obsdate_2m > obsdate_3p

			replace p3_order = 4 if obsdate_2m > obsdate_3p
			replace p3_order = 5 if obsdate_3p > obsdate_2m
			
			gen diff_m1 = confdate_1m - obsdate_1m
			gen diff_p1 = confdate_1p - obsdate_1p
			gen diff_m2 = confdate_2m - obsdate_2m
			gen diff_p2 = confdate_2p - obsdate_2p
			gen diff_p3 = confdate_3p - obsdate_3p
			
			replace diff_m1 = . if diff_m1 < 0
			replace diff_m2 = . if diff_m2 < 0
			replace diff_p1 = . if diff_p1 < 0
			replace diff_p2 = . if diff_p2 < 0
			replace diff_p3 = . if diff_p3 < 0
			
			gen log_ltsusp = log(1+ltsusp)
			gen log_stsusp = log(1+stsusp)
			gen experience_squared = experience * experience
			
			egen _sch_x_year = concat(sch_id year)
			
			replace haz_unif_m1 = 0 if year == 2010
			
			label var postconf_p1 "P1 Post-Feedback (Days)"
			label var postconf_p2 "P2 Post-Feedback (Days)"
			label var postconf_p3 "P3 Post-Feedback (Days)"
			label var postconf_m1 "M1 Post-Feedback (Days)"
			label var postconf_m2 "M2 Post-Feedback (Days)"
			
			label var haz_m1_true "M1 Cumulative Monitoring Intensity"	 
			label var haz_m2 "M2 Cumulative Monitoring Intensity"
			label var haz_p2 "P2 Cumulative Monitoring Intensity"
			
			label var haz_unif_m1 "M1 Cumulative Monitoring Intensity"
			label var haz_unif_m2 "M2 Cumulative Monitoring Intensity"
			label var haz_unif_p2 "P2 Cumulative Monitoring Intensity"
			
			label var haz_cycle1 "Window 1 Cumulative Monitoring Intensity"
			label var haz_cycle2 "Window 2 Cumulative Monitoring Intensity"
			label var haz_cycle3 "Window 3 Cumulative Monitoring Intensity"
				
			
			label var white "White"
			label var black "Black"
			label var hisp "Hispanic"
			label var othrace "Other Race"
			
			label var tblack "Black"
			label var twhite "White"
			label var thisp "Hispanic"
			label var tasian "Asian"
			label var taiak "Native American or Alaskan Native"
			
			label var sped_dv "Special Education"
			label var lep_dv "English Learner"
			label var frpl_dv "Free or Reduced-Price Lunch"
			label var experience "Experience"
			label var tlf_L "Evaluation Score (t-1)"
			
			label define yearlabel 2010 "2010" 2011 "2011" 2012 "2012"
			label values year yearlabel
			
			label var exp0 "First-Year"
			label define exp0label 0 "Veteran" 1 "First-Year"
			label values exp0 exp0label
			
			/*
				gen frac_ntc1 = nothreat_cycle1_true/max_ntp1
				gen frac_ntc2 = nothreat_cycle2/(max_ntm1-max_ntp1)
				gen frac_ntc3 = nothreat_cycle3/(-max_ntp2+max_ntm1)
				
				gen frac_pcp1 = (postconf_p1-max_ntp2)/max_ntp1
				gen frac_pcp2 = postconf_p2/max_ntp2
				gen frac_pcm1 = (postconf_m1-max_ntm2)/max_ntm1
				gen frac_pcm2 = (postconf_m2)/max_ntm2
			*/
			
				gen frac_ntc1 = nothreat_cycle1_true/(max_ntp1 + max_ntp2)
				gen frac_ntc2 = nothreat_cycle2/(max_ntp1 + max_ntp2)
				gen frac_ntc3 = nothreat_cycle3/(max_ntp1 + max_ntp2)
			
				gen frac_pcp1 = (postconf_p1-max_ntp2)/(max_ntp1 + max_ntp2)
				gen frac_pcp2 = postconf_p2/(max_ntp1 + max_ntp2)
				gen frac_pcm1 = (postconf_m1-max_ntm2)/(max_ntp1 + max_ntp2)
				gen frac_pcm2 = (postconf_m2)/(max_ntp1 + max_ntp2)
				
				gen y2010 = year == 2010
				gen ynot2010 = !y2010
				gen postconf_m1_not2010 = postconf_m1*ynot2010
				gen postconf_m1_2010 = postconf_m1*y2010
				
				foreach var in postconf_p1 postconf_p2 postconf_p3 postconf_m1 postconf_m2 {
					cap drop _mean
					bysort year: egen _mean = mean(`var')
					g `var'_mc = `var' - _mean
				}
				
			if "`framename'" != "grade_3_no_exp0"{
				xtile math_t_cat = math_std_t, nquantiles(5)
				xtile read_t_cat = read_std_t, nquantiles(5)
				
				egen grade_c = group(grade)
				label define gradelab 1 "4th Grade" 2 "5th Grade"
				label var grade_c "Grade Category"
				label values grade_c gradelab
			}
			global thisframe "`framename'"
			do "$code_dir\00_CreateInsample.do"
			
			keep if insample

			* get rid of duplicate students -- > students may have switched classes or schools
			duplicates tag stu_id year subject, g(_stu_dup)
			bysort stu_id year subject: egen _most_dose = max(year_dose)
			*bysort stu_id year subject (year_dose): g _most_dose = _n == _N \\ THIS IS VERY BAD -- leads to different results with every run
			drop if year_dose != _most_dose & _stu_dup > 0
			
			
			* Score variances
			bysort id year: egen math_var = sd(math_std) if subject == "Math"
			bysort id year: egen math_var_t = sd(math_std_t) if subject == "Math"

			bysort id year: egen read_var = sd(read_std) if subject == "ELA"
			bysort id year: egen read_var_t = sd(read_std_t) if subject == "ELA"

			* Score inter-quartile differences
			bysort id year: egen math_iqr = iqr(math_std) if subject == "Math"
			bysort id year: egen math_iqr_t = iqr(math_std_t) if subject == "Math"

			bysort id year: egen read_iqr = iqr(read_std) if subject == "ELA"
			bysort id year: egen read_iqr_t = iqr(read_std_t) if subject == "ELA"
			
			bysort id year: egen math_mdev = mdev(math_std) if subject == "Math"
			bysort id year: egen math_mdev_t = mdev(math_std_t) if subject == "Math"

			bysort id year: egen read_mdev = mdev(read_std) if subject == "ELA"
			bysort id year: egen read_mdev_t = mdev(read_std_t) if subject == "ELA"
			
			label var math_mdev "Math Mean Absolute Deviation"
			label var read_mdev "Reading Mean Absolute Deviation"
			
			
			
			forvalues i=1/9 {
				label var pr`i'_2m_std "Standard `i' Score (M2)"
				label var pr`i'_2p_std "Standard `i' Score (P2)"
			}
			
			label var nothreat_cycle1_true "Unmonitored Window 1"
			label var nothreat_cycle2 "Unmonitored Window 2"
			label var nothreat_cycle3 "Unmonitored Days"
			
			* impute for pr6
			*
			
			
		}
		global prevvars $tmpprevvars
	}
	
	
	/*
	frame copy grades_45_no_exp0 grades_45_no_exp0_nodups
	frame grades_45_no_exp0_nodups {
		duplicates tag stu_id year subject, g(_stu_dup)
		bysort stu_id year subject (year_dose): g _most_dose = _n == _N
		drop if !_most_dose & _stu_dup > 0
	}
	
	
	
	
	
	